<?php
/**
 * 文件名：login.mod.php
 * 版本号：(1.0)
 * 最后修改时间：2006年8月22日 18:58:20
 * 作者：狐狸<foxis@qq.com>
 * 功能描述：用户登录
 */

define('CUR_CLASS_OBJ', 'login');

//http://www.my347.com/pt_api/index.php?method=login.login&account=&pwd=&user_token=9a6f17da270a0e6a5affb606ebed0579&user_sign=&ver=1.0&op_net=-1&device=-1

class ModuleObject extends MasterObject
{
	var $curClass = '';
	var $curProcessing = '';
	
	var $playerAccount='';
	var $playerPassword ='';
	var $otherPassword ='';
	var $playerToken ='';
	var $userSign ='';
	var $version ='';
	var $op_net ='';
	var $device ='';
	var $timestamp ='';
	
	var $player_level =1;
	var $player_money =1000;
	var $player_gold =100;
	
	var $gamedata =array();
	var $Data = array();
	var $Event = array();
	
	function ModuleObject(&$data)
	{	
		$this->gamedata =&$data;
		$this->curClass = $data['class'];
		$this->curProcessing = $data['processing'];
		
		$this->playerAccount =get_param('user_account');
		$this->playerToken =get_param('user_token');
		
		$this->userSign =get_param('user_sign');
		$this->version =get_param('ver');
		$this->op_net =get_param('op_net');
		$this->device =get_param('device');
		$this->timestamp =get_param('timestamp');
		
		$this->Execute();
	}
	
	function Execute()
	{
		switch($this->curProcessing)
		{
			case 'register':
				$this->Register();
				break;
			case 'login':
				$this->Login();
				break;
			case 'freeLogin':
				$this->FreeLogin();
				break;
			case 'createrole':
				$this->CreateRole();
				break;
			default:
				exit($this->curProcessing.' is not exists in game login');
				break;
		}
	}
    
	function Register()
	{
		$pwd1 =get_param('pwd1');
		$this->playerPassword =get_param('pwd');
		$this->playerAccount =get_param('account');
		$this->playerToken =get_param('user_token');
		
		if($this->playerAccount =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id006";
			return false;
		}
		if($this->playerPassword =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id007";
			return false;
		}
		if($this->playerPassword !=$pwd1)
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id007";
			return false;
		}
		
		if($this->playerAccount !='' && $this->playerPassword !="")
		{
			$sql ="SELECT * FROM ".DB::table("bd_player")." WHERE player_account='{$this->playerAccount}' AND player_password='{$this->playerPassword}'";
			$MemberFields = DB::fetch_first($sql);
			if(!empty($MemberFields))
			{
				$this->gamedata['rc'] =1;
				$this->gamedata['warring_msg_id'] ="warring_id005";
				return false;
			}
		}
		
		$player_data = array(
			'player_account' => $this->playerAccount,
			'player_password' => $this->playerPassword,
			'player_otherpassword' => $pwd1,
			'player_token' => $this->playerToken,
				
			'player_name' => "sherry",
			'player_sex' => "male",
			'player_icon' =>"boy_01",
				
			'role_name' 	=> "2B小样",
			'role_id' 		=> "male_id001",	
			'role_level' 	=> 1,
				
			'shop_name'     =>"店铺未创建",
			'shop_level'    =>0,
			'shop_attack_count' => 1,
			'shop_steal_count' => 1,
				
			'friends_count' => 0,
			'friends_top' => 10,
			'enemy_count' => 0,
			'enemy_top' => 5,
				
			'player_level' => 1,
			'player_money' => 1000,
			'player_gold' => 100,
			'player_exp'    => 0,
				
			'player_sign' => $this->userSign,
			'player_version' => $this->version,
			'op_net' => $this->op_net,
			'player_device' => $this->device,	
			'player_login_time' => strtotime("now"),
			'player_login_ip' => GetIP(),
		);
		
		$cid = DB::insert('bd_player', $player_data, true);
		if($cid ==0)
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id008";
			return false;
		}
		
		$playerData =array();
		$sql ="SELECT * FROM ".DB::table("bd_player")." WHERE player_account='{$this->playerAccount}' AND player_password='{$this->playerPassword}'";
		$playerData = DB::fetch_first($sql);
		if(!empty($playerData))
		{
			$palyer =array();
			$palyer['account'] = $playerData['player_account'];
			$palyer['password'] = $playerData['player_password'];
			$palyer['token'] = $playerData['player_token'];
			
			$palyer['name'] 	= $playerData['player_name'];
			$palyer['level'] 	= (int)$playerData['player_level'];
			$palyer['money'] 	= (int)$playerData['player_money'];
			$palyer['gold'] 	= (int)$playerData['player_gold'];
			$palyer['sex'] 		= $playerData['player_sex'];
			$player['login_time'] =$playerData["player_login_time"];
			
			$palyer['friends_count']=(int)$playerData['friends_count'];
			$palyer['friends_top'] 	=(int)$playerData['friends_top'];
			$palyer['enemy_count'] 	=(int)$playerData['enemy_count'];
			$palyer['enemy_top'] 	=(int)$playerData['enemy_top'];
			
			$this->Data["player"] =$palyer;
		}
		else 
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id008";
			return false;
		}
		
		return true;
	}
	
	//http://www.my347.com/pt_api/index.php?method=login.login&account=&pwd=&user_token=9a6f17da270a0e6a5affb606ebed0579&user_sign=&ver=1.0&op_net=-1&device=-1
	function Login()
	{
		$this->playerPassword =get_param('pwd');
		$this->playerAccount =get_param('account');
		$this->playerToken =get_param('user_token');
		
		if($this->playerAccount =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id006";
			return false;
		}
		if($this->playerPassword =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id007";
			return false;
		}
		
		$sql = '';
		$MemberFields =array();
		if($this->playerAccount !='' && $this->playerPassword !="")
		{
			$sql ="SELECT * FROM ".DB::table("bd_player")." WHERE player_account='{$this->playerAccount}' AND player_password='{$this->playerPassword}'";
		}
		if($sql !='')
		{
			$MemberFields = DB::fetch_first($sql);
		}
		
		if(empty($MemberFields))
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id001";	
			return false;
		}
		
		$palyer =array();
		$palyer['account'] 	= $MemberFields['player_account'];
		$palyer['password'] = $MemberFields['player_password'];
		$palyer['token'] 	= $MemberFields['player_token'];
		
		$palyer['name'] 	= $MemberFields['player_name'];
		$palyer['level'] 	= (int)$MemberFields['player_level'];
		$palyer['money'] 	= (int)$MemberFields['player_money'];
		$palyer['gold'] 	= (int)$MemberFields['player_gold'];
		$palyer['sex'] 		= $MemberFields['player_sex'];
		
		$palyer['friends_count']=(int)$MemberFields['friends_count'];
		$palyer['friends_top'] 	=(int)$MemberFields['friends_top'];
		$palyer['enemy_count'] 	=(int)$MemberFields['enemy_count'];
		$palyer['enemy_top'] 	=(int)$MemberFields['enemy_top'];
		
		$player['login_time'] =$MemberFields["player_login_time"];
		
		$this->Data["player"] =$palyer;
		
		return true;
	}
	
	function CreateRole()
	{
		$name =get_param('name');
		$sex =get_param('sex');
		$icon =get_param('icon');
		$roleid =get_param('roleid');
		
		$this->playerAccount =get_param('user_account');
		$this->playerToken =get_param('user_token');
		$this->playerPassword =get_param('user_password');
		
		if($name =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id006";
			return false;
		}
		if($sex =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id007";
			return false;
		}
		if($icon =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id007";
			return false;
		}
		if($this->playerAccount =='' && $this->playerToken =='')
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id007";
			return false;
		}
		
		if($this->playerAccount !='' && $this->playerToken !="")
		{
			$sql ="SELECT * FROM ".DB::table("bd_player")." WHERE player_account='{$this->playerAccount}' AND player_token='{$this->playerToken}'";
			$MemberFields = DB::fetch_first($sql);
			if(empty($MemberFields))
			{
				$this->gamedata['rc'] =1;
				$this->gamedata['warring_msg_id'] ="warring_id005";
				return false;
			}
		}
		
		$setarr = array(
			'player_name' 	=> $name,
			'player_sex' 	=> $sex,
			'player_icon' 	=> $icon,
			'player_level'	=> $this->player_level,
			'player_money'	=> $this->player_money,
			'player_gold' 	=> $this->player_gold,
			'player_exp'    => 0,
					
			'role_name' 	=> "2B小样",
			'role_id' 		=> $roleid,
			'role_level' 	=> 1,
			'shop_name'     =>"店铺未创建",
			'shop_level'    =>0,
			'shop_attack_count' => 1,
			'shop_steal_count' => 1,	
				
			'friends_count' => 0,
			'friends_top' => 10,
			'enemy_count' => 0,
			'enemy_top' => 5,
				
			'player_sign' 	=> $this->userSign,
			'player_version' => $this->version,
			'op_net' 	=> $this->op_net,
			'player_device' => $this->device,
			'player_login_time' => strtotime("now"),
			'player_login_ip' => GetIP(),
		);
		$condition =array(
			'player_account' =>	$this->playerAccount,
			'player_token' =>$this->playerToken,		);
		$return = DB::update("bd_player", $setarr, $condition);
		if(!$return)
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id008";
			return false;
		}
		
		$playerData =array();
		$sql ="SELECT * FROM ".DB::table("bd_player")." WHERE player_account='{$this->playerAccount}' AND player_token='{$this->playerToken}'";
		$playerData = DB::fetch_first($sql);
		if(!empty($playerData))
		{
			$palyer =array();
			$palyer['account'] = $playerData['player_account'];
			$palyer['password'] = $playerData['player_password'];
			$palyer['token'] = $playerData['player_token'];
		
			$palyer['name']  =$playerData['player_name'];
			$palyer['level'] =(int)$playerData['player_level'];
			$palyer['money'] =(int)$playerData['player_money'];
			$palyer['gold'] =(int)$playerData['player_gold'];
			$palyer['sex'] 	=$playerData['player_sex'];
			$palyer['icon'] =$playerData['player_icon'];
			$palyer['exp'] =(int)$playerData['player_exp'];
			
			$palyer['friends_count']=(int)$playerData['friends_count'];
			$palyer['friends_top'] 	=(int)$playerData['friends_top'];
			$palyer['enemy_count'] 	=(int)$playerData['enemy_count'];
			$palyer['enemy_top'] 	=(int)$playerData['enemy_top'];
			
			$player['login_time'] =$playerData["player_login_time"];
			$this->Data["player"] =$palyer;
		}
		else
		{
			$this->gamedata['rc'] =1;
			$this->gamedata['warring_msg_id'] ="warring_id008_11";
			return false;
		}
		
		return true;
	}
	
	function  FreeLogin()
	{
		
	}
	
	function GameData()
	{
		return $this->Data;
	}
	
	function GameEvent()
	{
		return $this->Event;
	}
	
}
?>